package com.tengju.user.infrastructure.repository.archive;

import com.tengju.user.domain.model.archive.IdCardNo;
import com.tengju.user.domain.model.archive.letax.LetaxAccount;
import com.tengju.user.domain.model.archive.letax.LetaxAccountId;
import com.tengju.user.domain.model.archive.letax.LetaxRepository;
import com.tengju.user.domain.model.archive.letax.LetaxStatus;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.AccountId;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.LetaxAccountDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.generate.LetaxAccountDOMapper;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.generate.LetaxAccountWhiteDOMapper;
import com.tengju.user.infrastructure.repository.convert.LetaxAccountConvert;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import org.springframework.stereotype.Repository;

import java.util.Set;

@Repository
public class LetaxRepositoryImpl implements LetaxRepository {

    private final LetaxAccountDOMapper letaxAccountDOMapper;

    public LetaxRepositoryImpl(LetaxAccountDOMapper letaxAccountDOMapper) {
        this.letaxAccountDOMapper = letaxAccountDOMapper;
    }

    @Override
    public LetaxAccount find(UserIdCode idCode, AccountId accountId) {
        LetaxAccountDO letaxAccountDO = letaxAccountDOMapper
                .findOne(idCode.getValue(), accountId.getValue(),
                        LetaxStatus.TERMINATED.getVal());
        return LetaxAccountConvert.convert(letaxAccountDO);
    }

    @Override
    public LetaxAccount findOneByStatus(UserIdCode idCode, AccountId accountId, LetaxStatus status) {
        LetaxAccountDO letaxAccountDO = letaxAccountDOMapper
                .findOneByStatus(idCode.getValue(), accountId.getValue(), status.getVal());
        return LetaxAccountConvert.convert(letaxAccountDO);
    }

    @Override
    public LetaxAccount findOne(UserIdCode idCode, AccountId accountId, LetaxStatus notStatus) {
        LetaxAccountDO letaxAccountDO = letaxAccountDOMapper
                .findOne(idCode.getValue(),
                        accountId.getValue(),
                        notStatus.getVal());
        return LetaxAccountConvert.convert(letaxAccountDO);
    }

    @Override
    public LetaxAccount find(UserIdCode idCode, LetaxStatus notStatus) {
        LetaxAccountDO letaxAccountDO = letaxAccountDOMapper
                .findByIdCodeAndNotStatus(idCode.getValue(), notStatus.getVal());
        return LetaxAccountConvert.convert(letaxAccountDO);
    }

    @Override
    public int count(UserIdCode idCode, IdCardNo idCardNo, LetaxStatus notStatus) {
        return letaxAccountDOMapper.count(idCode.getValue(), idCardNo.getValue() ,notStatus.getVal());
    }

    @Override
    public int update(LetaxAccountId letaxAccountId, LetaxAccount letaxAccount) {
        LetaxAccountDO letaxAccountDO = LetaxAccountConvert.convert(letaxAccountId, letaxAccount);
        return letaxAccountDOMapper.updateByPrimaryKeySelective(letaxAccountDO);
    }

    @Override
    public long save(LetaxAccount letaxAccount) {
        LetaxAccountDO letaxAccountDO = LetaxAccountConvert.convert(letaxAccount);
        letaxAccountDOMapper.insertSelective(letaxAccountDO);
        return letaxAccountDO.getId();
    }

    @Override
    public int count(IdCardNo idCardNo, LetaxStatus notStatus) {
        return 0;
    }

    @Override
    public LetaxAccount findByIdCardNoAndNotStatus(IdCardNo idCardNo, LetaxStatus notStatus) {
        LetaxAccountDO letaxAccountDO = letaxAccountDOMapper
                .findByIdCardNoAndNotStatus(idCardNo.getValue(), notStatus.getVal());
        return LetaxAccountConvert.convert(letaxAccountDO);
    }


    @Override
    public LetaxAccount findOneNotIn(UserIdCode idCode, AccountId accountId,
            List<LetaxStatus> notInStatus) {
        LetaxAccountDO letaxAccountDO = letaxAccountDOMapper
                .findOneByIdCodeAndAccountIdAndNotStatus(idCode.getValue(),
                        accountId.getValue(),
                        notInStatus.stream().map(LetaxStatus::getVal).collect(Collectors.toList()));
        return LetaxAccountConvert.convert(letaxAccountDO);
    }
}
